import redis
import json

pool = redis.ConnectionPool(host='localhost', port=6379)
r = redis.Redis(connection_pool=pool)

def session_get(sid, key):
    global r
    dd = r.get(sid)
    if dd:
        # print('dd', dd)
        jj = json.loads(dd)
        # print('jjj', jj)
        if key in jj:
            return jj[key]

    else:
        return None


def session_set(sid, key, value):
    global r
    dd = r.get(sid)
    if not dd:
        dd = '{}'

    jj = json.loads(dd)
    # print(type(jj))
    jj[key] = value
    ddj = json.dumps(jj)
    r.set(name=sid, value=ddj, ex=600)


def session_clear(sid):
    global r
    r.set(sid, None)


def redis_set(key, value):
    global r
    r.set(key, value)

def redis_get(key):
    global r
    return r.get(key)